if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
var container, cW = 720, cH = 380;
var camera, scene, renderer;

// 计算体积和表面积
function calc_vol_and_area(geo) {
    var x1, x2, x3, y1, y2, y3, z1, z2, z3, i;
    var len = geo.faces.length;
    var totalVolume = 0;
    var totalArea = 0;
    var a, b, c, s;
    var toz, tox, toy;
    for (i = 0; i < len; i++) {
        x1 = geo.vertices[geo.faces[i].a].x;
        y1 = geo.vertices[geo.faces[i].a].y;
        z1 = geo.vertices[geo.faces[i].a].z;
        x2 = geo.vertices[geo.faces[i].b].x;
        y2 = geo.vertices[geo.faces[i].b].y;
        z2 = geo.vertices[geo.faces[i].b].z;
        x3 = geo.vertices[geo.faces[i].c].x;
        y3 = geo.vertices[geo.faces[i].c].y;
        z3 = geo.vertices[geo.faces[i].c].z;
        totalVolume += -x3 * y2 * z1 + x2 * y3 * z1 + x3 * y1 * z2 - x1 * y3 * z2 - x2 * y1 * z3 + x1 * y2 * z3;
        a = geo.vertices[geo.faces[i].a].distanceTo(geo.vertices[geo.faces[i].b]);
        b = geo.vertices[geo.faces[i].b].distanceTo(geo.vertices[geo.faces[i].c]);
        c = geo.vertices[geo.faces[i].c].distanceTo(geo.vertices[geo.faces[i].a]);
        if (!toz) {
            toz = Math.min(a, b, c)
        } else {
            toz = Math.min(toz, a, b, c)
        }
        s = (a + b + c) / 2;
        if (String(Math.sqrt(s * (s - a) * (s - b) * (s - c))) == "NaN") {
            continue
        }
        totalArea += Math.sqrt(s * (s - a) * (s - b) * (s - c))
    }
    // alert("totalArea" + totalArea)
    return [Math.abs(totalVolume / 6), totalArea, toz]
}

scene = new THREE.Scene();
scene.add( new THREE.AmbientLight( 0x999999 ) );

var grid = new THREE.GridHelper( 50, 50, 0xffffff, 0x555555 );
grid.rotateOnAxis( new THREE.Vector3( 1, 0, 0 ), 90 * ( Math.PI/180 ) );
scene.add( grid );

camera = new THREE.PerspectiveCamera( 45, cW / cH, 1, 500 );
camera.up.set( 0, 0, 1 );
camera.position.set( 0, -9, 6 );
camera.add( new THREE.PointLight( 0xffffff, 0.8 ) );
scene.add( camera );


//scene.add( new THREE.HemisphereLight( 0x443333, 0x111122 ) );

renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setClearColor( 0x999999 );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( cW, cH );

function init(divId) {
	container = document.getElementById(divId);
	container.appendChild( renderer.domElement );
	
	var loader = new THREE.STLLoader();
	loader.load( 'https://threejs.org/examples/models/stl/binary/pr2_head_tilt.stl', function ( geometry ) {

		var material = new THREE.MeshPhongMaterial( { color: 0x0099FF, specular: 0x111111, shininess: 200 } );
		var mesh = new THREE.Mesh( geometry, material );
		// 控制选择角度
		var mesh = new THREE.Mesh( geometry, material );
		mesh.position.set( 0, 0, 1.5 );
		mesh.rotation.set( - Math.PI / 2, 0, 0 );
		mesh.scale.set( 10, 10, 10 );
		mesh.castShadow = true;
		mesh.receiveShadow = true;
		scene.add( mesh );
		render();
		
		// 计算的分水岭
		var attrib = geometry.getAttribute("position");
    	if (attrib === undefined) {
            throw new Error("缺少位置属性")
        }
    	var positions = attrib.array;
        var vertices = [];
        for (var i = 0,
            n = positions.length; i < n; i += 3) {
            var x = positions[i];
            var y = positions[i + 1];
            var z = positions[i + 2];
            vertices.push(new THREE.Vector3(x, y, z))
        }
        var faces = [];
        for (var i = 0,
                 n = vertices.length; i < n; i += 3) {
            faces.push(new THREE.Face3(i, i + 1, i + 2))
        }
        geometry.vertices = vertices;
        geometry.faces = faces;
        geometry.computeFaceNormals();
        // geometry.mergeVertices();
        geometry.computeVertexNormals();
        THREE.GeometryUtils.center(geometry);
        
        // mesh.useQuaternion = true;
        var axis = new THREE.Vector3(100, 100, 100).normalize();
        var angle = Math.PI / .3;
        var q = (new THREE.Quaternion).setFromAxisAngle(axis, angle);
        //mesh.quaternion.copy(q);
        //mesh.castShadow = true;
        //mesh.receiveShadow = true;
        //scene.add(mesh);
        xsize = geometry.boundingBox.max.x - geometry.boundingBox.min.x;
        ysize = geometry.boundingBox.max.y - geometry.boundingBox.min.y;
        zsize = geometry.boundingBox.max.z - geometry.boundingBox.min.z;
        
        // alert(xsize + ":" + ysize + ":" + zsize);
        vol_area = calc_vol_and_area(geometry);
        // alert("geometry____: " + vol_area);
	} );

	var controls = new THREE.OrbitControls( camera, renderer.domElement );
	controls.addEventListener( 'change', render );
	controls.target.set( 0, 1.2, 2 );
	controls.update();
	
	/* window.addEventListener( 'resize', onWindowResize, false ); */
}

function onWindowResize() {
	render();
}

function animate() {
	/*requestAnimationFrame( animate );*/
	render();
}

function render() {
	renderer.render( scene, camera );
}
